#include "uqueue_LiteQueue.h"
#include "pika_platform_thread.h"
#include "threading_Semaphore.h"
#include "pika_hal_cqueue.h"

void uqueue_LiteQueue___init__(PikaObj *self, int qsize, int esize){
    if (qsize <= 0 || esize <= 0){
        // TODO 异常
        obj_raise(self, PIKA_RES_ERR_VALUE_ERROR, "greater than 0 is required.");
        return;
    }
    pika_hal_cqueue_t* que = (pika_hal_cqueue_t*)pikaMalloc(sizeof(pika_hal_cqueue_t));
    pika_platform_thread_semaphore_t* sem = pikaMalloc(sizeof(pika_platform_thread_semaphore_t));
    
    int err = pika_hal_cqueue_init(que, qsize, esize);

    if (que == NULL || sem == NULL || err < 0){
        if (que != NULL){
            if (err == 0){
                pika_hal_cqueue_deinit(que);
            }
            pikaFree(que, sizeof(pika_hal_cqueue_t));
        }
        if (sem != NULL){
            pikaFree(sem, sizeof(pika_platform_thread_semaphore_t));
        }
        obj_raise(self, PIKA_RES_ERR_MEMORY_ERROR, "not enough memory.");
        return;
    }
    
    
    obj_setPtr(self, "queue", que);

}

pika_bool uqueue_LiteQueue_empty(PikaObj *self){

}
pika_bool uqueue_LiteQueue_full(PikaObj *self){

}
int uqueue_LiteQueue_get(PikaObj *self, pika_bool block, Arg* timeout){

}
void uqueue_LiteQueue_put(PikaObj *self, int item, pika_bool block, Arg* timeout){

}

void uqueue_LiteQueue___del__(PikaObj *self){
    pika_hal_cqueue_t* que = obj_getPtr(self, "queue");
    pika_hal_cqueue_deinit(que);
    pikaFree(que, sizeof(pika_hal_cqueue_t));

}